package cn.bdqn.service;

import cn.bdqn.entity.Customer;
import cn.bdqn.mapper.CustomerMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @Description TODO
 * @Date 2020/2/10 12:04
 * @Created by 张三三
 */
public class CustomerServiceImpl {

    public List<Customer> selectAll() throws IOException {
        InputStream is = Resources.getResourceAsStream("configuration.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        SqlSession session = factory.openSession(true);

        CustomerMapper mapper = session.getMapper(CustomerMapper.class);

        Page<Customer> page = PageHelper.startPage(1, 10,"cid desc");
        List<Customer> customers = mapper.selectAll();
        return customers;
    };

    public boolean deleteById(long cid) throws IOException {
        InputStream is = Resources.getResourceAsStream("configuration.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        SqlSession session = factory.openSession(true);

        CustomerMapper mapper = session.getMapper(CustomerMapper.class);

        return mapper.deleteById(cid);
    };
}
